package csu.web.mypetstore.web.servlet;

import csu.web.mypetstore.domain.Category;
import csu.web.mypetstore.domain.Product;
import csu.web.mypetstore.service.CatalogService;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.List;

public class CategoryFormServlet extends HttpServlet {

    //    private String categoryId;
    private static final String CATEGORY_FORM = "/WEB-INF/jsp/catalog/category.jsp";

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String categoryId = req.getParameter("categoryId");
        CatalogService catalogService = new CatalogService();
        HttpSession httpSession = req.getSession();


        Category category = catalogService.getCategory(categoryId);
        httpSession.setAttribute("category", category);
        List<Product> productList = catalogService.getProductListByCategory(categoryId);
        httpSession.setAttribute("productList", productList);



        req.getRequestDispatcher(CATEGORY_FORM).forward(req, resp);
    }
}